﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.MediaCenter.Samples.MediaState;

namespace omaura_oled
{
    /// <summary>
    /// Send the Data to the OLED
    /// </summary>
    public class IconsManager
    {
        #region "Attributes"
        //--
        // Scrolling Parameters
        private Bitmap scrollingText;
        private int scrollingTextPosition = 0;
        private int scrollingTextHeight = 0;
        private int scrollingTextWidth = 0;
        private int scrollingPosX = 0;
        private int scrollingPosY = 0;
        private bool scrollingRightToLeft = true;
        // Image
        private byte[][] imageToDisplay;
        // Backgrounds
        private byte[][] iconBkgnd_Death;
        private byte[][] iconBkgnd_DVD;
        private byte[][] iconBkgnd_CD;
        private byte[][] iconBkgnd_PVR;
        private byte[][] iconBkgnd_Audio;
        private byte[][] iconBkgnd_Video;
        private byte[][] iconBkgnd_TVTuner;
        private byte[][] iconBkgnd_Photos;
        private byte[][] iconBkgnd_Radio;
        private byte[][] iconBkgnd_Home;
        private byte[][] iconBkgnd_Home2;
        // Icons
        private byte[][] iconSound;
        private Size iconSoundSize;
        private byte[][] iconPlay;
        private Size iconPlaySize;
        private byte[][] iconPause;
        private Size iconPauseSize;
        private byte[][] iconStop;
        private Size iconStopSize;
        private byte[][] iconRecord;
        private Size iconRecordSize;
        private byte[][] iconRW;
        private Size iconRWSize;
        private byte[][] iconFF;
        private Size iconFFSize;
        //-- Logo
        private byte[][] iconLogo;
        private Size iconLogoSize;
        // Status
        private bool iconsAreLoaded = false;
        // Link to the App
        private OmauraOled parent;
        //--
        #endregion "Attributes"

        #region "Constructor"
        //--
        public IconsManager(OmauraOled formRec)
        {
            parent = formRec;
        }
        //--
        #endregion "Constructor"

        #region "Methods"
        //--
        public void sendImage(string pathToImage)
        {
            if (parent.serialPort1.IsOpen)
            {
                int i, j, x, y;
                int nbBytes = 8192;
                byte[] buffer = new byte[nbBytes];
                buffer[0] = 0x70; // Start code
                buffer[1] = 0x81; // Icon code
                parent.serialPort1.Write(buffer, 0, 2);

                //-- init image, and imageToDisplay[][]
                Bitmap imageTemp = new Bitmap(pathToImage);

                imageToDisplay = new byte[64][];
                for (i = 0; i < 64; i++)
                    imageToDisplay[i] = new byte[256];
                for (y = 0; y < 64; y++)
                {
                    for (x = 0; x < 256; x++)
                        imageToDisplay[y][x] = (byte)convertColor(System.Drawing.ColorTranslator.ToWin32(imageTemp.GetPixel(x, y)));
                }

                //-- create the buffer and send it
                for (j = 0; j < 64; j++)
                {
                    for (i = 0; i < 128; i++)
                        buffer[i + j * 128] = (byte)((byte)(imageToDisplay[63 - j][i * 2] >> 4) + (byte)(imageToDisplay[63 - j][i * 2 + 1] & 0xF0));
                }
                buffer[nbBytes - 1] = 0;

                parent.serialPort1.Write(buffer, 0, nbBytes);

                /*
                int i, j, x ,y;
                int nbBytes = 8192;
                byte[] buffer = new byte[nbBytes];
                buffer[0] = 0x70; // Start code
                buffer[1] = 0x81; // Icon code
                buffer[2] = 16; // nbColors
                parent.serialPort1.Write(buffer, 0, 3);

                //-- init image, and imageToDisplay[][]
                Bitmap imageTemp = new Bitmap(pathToImage);

                imageToDisplay = new byte[64][];
                for (i = 0; i < 64; i++)
                    imageToDisplay[i] = new byte[256];
                for (y = 0; y < 64; y++)
                {
                    for (x = 0; x < 256; x++)
                        imageToDisplay[y][x] = (byte)convertColor(System.Drawing.ColorTranslator.ToWin32(imageTemp.GetPixel(x, y)));
                }

                //-- create the buffer and send it
                for (j = 0; j < 64; j++)
                {
                    for (i = 0; i < 128; i++)
                        buffer[i + j * 128] = (byte)((byte)(imageToDisplay[63 - j][i * 2] >> 4) + (byte)(imageToDisplay[63 - j][i * 2 + 1] & 0xF0));
                }
                parent.serialPort1.Write(buffer, 0, nbBytes);
                */
            }
        }
        public void sendLogo(string pathToImage)
        {
            int x, y;
            int i, j;
            int widthTemp;
            int nbBytes;
            byte[] buffer;
            byte part1, part2;
            System.Drawing.Bitmap myImage;

            //-- load logo into buffer
            myImage = new System.Drawing.Bitmap(pathToImage);
            iconLogoSize = new Size(myImage.Width, myImage.Height);
            iconLogo = new byte[myImage.Height][];
            for (i = 0; i < myImage.Height; i++)
                iconLogo[i] = new byte[myImage.Width];
            for (y = 0; y < myImage.Height; y++)
            {
                for (x = 0; x < myImage.Width; x++)
                    iconLogo[y][x] = (byte)convertColor(System.Drawing.ColorTranslator.ToWin32(myImage.GetPixel(x, y)));
            }

            //-- display logo
            if (parent.serialPort1.IsOpen)
            {
                widthTemp = iconLogoSize.Width / 2;
                if (widthTemp * 2 < widthTemp) widthTemp++;
                nbBytes = 8 + (iconLogoSize.Height * widthTemp);
                buffer = new byte[nbBytes];
                buffer[0] = 0x70; // Start code
                buffer[1] = 0x82; // Icon code
                buffer[2] = (byte)((128 - (iconLogoSize.Width / 2)) / 2);// posx; // x (0 - 127)
                buffer[3] = (byte)64;//((64 - iconLogoSize.Height) / 2);// posy; // y (0 - 63)
                buffer[4] = (byte)widthTemp; // Width
                buffer[5] = (byte)iconLogoSize.Height; // Height
                buffer[6] = 1; // vertical

                for (j = 0; j < iconLogoSize.Height; j++)
                {
                    for (i = 0; i < widthTemp; i++)
                    {
                        part1 = iconLogo[(iconLogoSize.Height - 1) - j][i * 2 + 1];
                        if (i + 1 < iconLogoSize.Width)
                            part2 = iconLogo[(iconLogoSize.Height - 1) - j][i * 2];
                        else
                            part2 = 0;
                        buffer[7 + i + j * widthTemp] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                    }
                }
                buffer[nbBytes - 1] = 0;

                parent.serialPort1.Write(buffer, 0, nbBytes);

                /*
                widthTemp = iconLogoSize.Width / 2;
                if (widthTemp * 2 < widthTemp) widthTemp++;
                nbBytes = 8 + (iconLogoSize.Height * widthTemp);
                buffer = new byte[nbBytes];
                buffer[0] = 0x70; // Start code
                buffer[1] = 0x82; // Icon code
                buffer[2] = (byte)((128 - (iconLogoSize.Width / 2)) / 2);// posx; // x (0 - 127)
                buffer[3] = (byte)64;//((64 - iconLogoSize.Height) / 2);// posy; // y (0 - 63)
                buffer[4] = (byte)widthTemp; // Width
                buffer[5] = (byte)iconLogoSize.Height; // Height
                buffer[6] = 1; // vertical
                buffer[7] = 16; // nbColors

                for (j = 0; j < iconLogoSize.Height; j++)
                {
                    for (i = 0; i < widthTemp; i++)
                    {
                        part1 = iconLogo[(iconLogoSize.Height - 1) - j][i * 2 + 1];
                        if (i + 1 < iconLogoSize.Width)
                            part2 = iconLogo[(iconLogoSize.Height - 1) - j][i * 2];
                        else
                            part2 = 0;
                        buffer[8 + i + j * widthTemp] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                    }
                }
                parent.serialPort1.Write(buffer, 0, nbBytes);
                */
            }
        }
        public void sendLogo(string pathToImage, int xPosition, int yPosition)
        {
            int x, y;
            int i, j;
            int widthTemp;
            int nbBytes;
            byte[] buffer;
            byte part1, part2;
            System.Drawing.Bitmap myImage;

            //-- load logo into buffer
            myImage = new System.Drawing.Bitmap(pathToImage);
            iconLogoSize = new Size(myImage.Width, myImage.Height);
            iconLogo = new byte[myImage.Height][];
            for (i = 0; i < myImage.Height; i++)
                iconLogo[i] = new byte[myImage.Width];
            for (y = 0; y < myImage.Height; y++)
            {
                for (x = 0; x < myImage.Width; x++)
                    iconLogo[y][x] = (byte)convertColor(System.Drawing.ColorTranslator.ToWin32(myImage.GetPixel(x, y)));
            }

            //-- display logo
            if (parent.serialPort1.IsOpen)
            {
                widthTemp = iconLogoSize.Width / 2;
                if (widthTemp * 2 < widthTemp) widthTemp++;
                nbBytes = 8 + (iconLogoSize.Height * widthTemp);

                buffer = new byte[nbBytes];
                buffer[0] = 0x70; // Start code
                buffer[1] = 0x82; // Icon code
                buffer[2] = (byte)(xPosition / 2);// posx; // x (0 - 127)
                buffer[3] = (byte)yPosition;// posy; // y (0 - 63)
                buffer[4] = (byte)widthTemp; // Width
                buffer[5] = (byte)iconLogoSize.Height; // Height
                buffer[6] = 1; // vertical

                for (j = 0; j < iconLogoSize.Height; j++)
                {
                    for (i = 0; i < widthTemp; i++)
                    {
                        part1 = iconLogo[(iconLogoSize.Height - 1) - j][i * 2 + 1];
                        if (i + 1 < iconLogoSize.Width)
                            part2 = iconLogo[(iconLogoSize.Height - 1) - j][i * 2];
                        else
                            part2 = 0;
                        buffer[7 + i + j * widthTemp] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                    }
                }
                buffer[nbBytes - 1] = 0;

                parent.serialPort1.Write(buffer, 0, nbBytes);

                /*
                widthTemp = iconLogoSize.Width / 2;
                if (widthTemp * 2 < widthTemp) widthTemp++;
                nbBytes = 8 + (iconLogoSize.Height * widthTemp);
                buffer = new byte[nbBytes];
                buffer[0] = 0x70; // Start code
                buffer[1] = 0x82; // Icon code
                buffer[2] = (byte)(xPosition / 2);// posx; // x (0 - 127)
                buffer[3] = (byte)yPosition;// posy; // y (0 - 63)
                buffer[4] = (byte)widthTemp; // Width
                buffer[5] = (byte)iconLogoSize.Height; // Height
                buffer[6] = 1; // vertical
                buffer[7] = 16; // nbColors

                for (j = 0; j < iconLogoSize.Height; j++)
                {
                    for (i = 0; i < widthTemp; i++)
                    {
                        part1 = iconLogo[(iconLogoSize.Height - 1) - j][i * 2 + 1];
                        if (i + 1 < iconLogoSize.Width)
                            part2 = iconLogo[(iconLogoSize.Height - 1) - j][i * 2];
                        else
                            part2 = 0;
                        buffer[8 + i + j * widthTemp] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                    }
                }
                parent.serialPort1.Write(buffer, 0, nbBytes);
                */
            }
        }

        //--
        public void sendBackgroundData(string bkgnd)
        {
            if (iconsAreLoaded && parent.serialPort1.IsOpen)
            {
                int i, j;
                int nbBytes = 8193;
                byte[] buffer = new byte[nbBytes];
                buffer[0] = 0x70; // Start code
                buffer[1] = 0x81; // Icon code
                parent.serialPort1.Write(buffer, 0, 2);

                /*
                int i, j;
                int nbBytes = 8192;
                byte[] buffer = new byte[nbBytes];
                buffer[0] = 0x70; // Start code
                buffer[1] = 0x81; // Icon code
                buffer[2] = 16; // nbColors
                parent.serialPort1.Write(buffer, 0, 3);
                */

                if (bkgnd == "Death")
                {
                    for (j = 0; j < 64; j++)
                    {
                        for (i = 0; i < 128; i++)
                            buffer[i + j * 128] = (byte)((byte)(iconBkgnd_Death[63 - j][i * 2] >> 4) + (byte)(iconBkgnd_Death[63 - j][i * 2 + 1] & 0xF0));
                    }
                    parent.serialPort1.Write(buffer, 0, nbBytes);
                }
                else if (bkgnd == "Home")
                {
                    for (j = 0; j < 64; j++)
                    {
                        for (i = 0; i < 128; i++)
                            //buffer[i + j * 128] = (byte)((byte)(iconBkgnd_Home[63 - j][i * 2 + 1] & 0xF0) + (byte)(iconBkgnd_Home[63 - j][i * 2] >> 4));
                            buffer[i + j * 128] = (byte)((byte)(iconBkgnd_Home[63 - j][i * 2] >> 4) + (byte)(iconBkgnd_Home[63 - j][i * 2 + 1] & 0xF0));                    
                    }
                    buffer[nbBytes - 1] = 0;

                    parent.serialPort1.Write(buffer, 0, nbBytes);
                }
                else if (bkgnd == "Home2")
                {
                    for (j = 0; j < 64; j++)
                    {
                        for (i = 0; i < 128; i++)
                            buffer[i + j * 128] = (byte)((byte)(iconBkgnd_Home2[63 - j][i * 2] >> 4) + (byte)(iconBkgnd_Home2[63 - j][i * 2 + 1] & 0xF0));
                    }
                    parent.serialPort1.Write(buffer, 0, nbBytes);
                }
                else if (bkgnd == "DVD")
                {
                    for (j = 0; j < 64; j++)
                    {
                        for (i = 0; i < 128; i++)
                            buffer[i + j * 128] = (byte)((byte)(iconBkgnd_DVD[63 - j][i * 2] >> 4) + (byte)(iconBkgnd_DVD[63 - j][i * 2 + 1] & 0xF0));
                    }
                    parent.serialPort1.Write(buffer, 0, nbBytes);
                }
                else if (bkgnd == "CD")
                {
                    for (j = 0; j < 64; j++)
                    {
                        for (i = 0; i < 128; i++)
                            buffer[i + j * 128] = (byte)((byte)(iconBkgnd_CD[63 - j][i * 2] >> 4) + (byte)(iconBkgnd_CD[63 - j][i * 2 + 1] & 0xF0));
                    }
                    parent.serialPort1.Write(buffer, 0, nbBytes);
                }
                else if (bkgnd == "PVR")
                {
                    for (j = 0; j < 64; j++)
                    {
                        for (i = 0; i < 128; i++)
                            buffer[i + j * 128] = (byte)((byte)(iconBkgnd_PVR[63 - j][i * 2] >> 4) + (byte)(iconBkgnd_PVR[63 - j][i * 2 + 1] & 0xF0));
                    }
                    parent.serialPort1.Write(buffer, 0, nbBytes);
                }
                else if (bkgnd == "Audio")
                {
                    for (j = 0; j < 64; j++)
                    {
                        for (i = 0; i < 128; i++)
                            buffer[i + j * 128] = (byte)((byte)(iconBkgnd_Audio[63 - j][i * 2] >> 4) + (byte)(iconBkgnd_Audio[63 - j][i * 2 + 1] & 0xF0));
                    }
                    parent.serialPort1.Write(buffer, 0, nbBytes);
                }
                else if (bkgnd == "Video")
                {
                    for (j = 0; j < 64; j++)
                    {
                        for (i = 0; i < 128; i++)
                            buffer[i + j * 128] = (byte)((byte)(iconBkgnd_Video[63 - j][i * 2] >> 4) + (byte)(iconBkgnd_Video[63 - j][i * 2 + 1] & 0xF0));
                    }
                    parent.serialPort1.Write(buffer, 0, nbBytes);
                }
                else if (bkgnd == "TVTuner")
                {
                    for (j = 0; j < 64; j++)
                    {
                        for (i = 0; i < 128; i++)
                            buffer[i + j * 128] = (byte)((byte)(iconBkgnd_TVTuner[63 - j][i * 2] >> 4) + (byte)(iconBkgnd_TVTuner[63 - j][i * 2 + 1] & 0xF0));
                    }
                    parent.serialPort1.Write(buffer, 0, nbBytes);
                }
                else if (bkgnd == "Photos")
                {
                    for (j = 0; j < 64; j++)
                    {
                        for (i = 0; i < 128; i++)
                            buffer[i + j * 128] = (byte)((byte)(iconBkgnd_Photos[63 - j][i * 2] >> 4) + (byte)(iconBkgnd_Photos[63 - j][i * 2 + 1] & 0xF0));
                    }
                    parent.serialPort1.Write(buffer, 0, nbBytes);
                }
                else if (bkgnd == "Radio")
                {
                    for (j = 0; j < 64; j++)
                    {
                        for (i = 0; i < 128; i++)
                            buffer[i + j * 128] = (byte)((byte)(iconBkgnd_Radio[63 - j][i * 2] >> 4) + (byte)(iconBkgnd_Radio[63 - j][i * 2 + 1] & 0xF0));
                    }
                    parent.serialPort1.Write(buffer, 0, nbBytes);
                }
            }
        }
        public void sendIcondata(string icon, int posx, int posy)
        {
            if (iconsAreLoaded && parent.serialPort1.IsOpen)
            {
                int i, j;
                int widthTemp;
                int nbBytes;
                byte[] buffer;
                byte part1, part2;

                if (icon == "Sound")
                {
                    widthTemp = iconSoundSize.Width / 2;
                    if (widthTemp * 2 < iconSoundSize.Width) widthTemp++;
                    nbBytes = 8 + (iconSoundSize.Height * widthTemp);
                    buffer = new byte[nbBytes];
                    buffer[0] = 0x70; // Start code
                    buffer[1] = 0x82; // Icon code
                    buffer[2] = (byte)posx; // x (0 - 127)
                    buffer[3] = (byte)posy; // y (0 - 63)
                    buffer[4] = (byte)widthTemp; // Width
                    buffer[5] = (byte)iconSoundSize.Height; // Height
                    buffer[6] = 1; // vertical

                    for (j = 0; j < iconSoundSize.Height; j++)
                    {
                        for (i = 0; i < widthTemp; i++)
                        {
                            part1 = iconSound[(iconSoundSize.Height - 1) - j][i * 2 + 1];
                            if (i + 1 < iconSoundSize.Width)
                                part2 = iconSound[(iconSoundSize.Height - 1) - j][i * 2];
                            else
                                part2 = 0;
                            buffer[7 + i + j * widthTemp] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                        }
                    }
                    buffer[nbBytes - 1] = 0;

                    parent.serialPort1.Write(buffer, 0, nbBytes);

                    /*
                    widthTemp = iconSoundSize.Width / 2;
                    if (widthTemp * 2 < iconSoundSize.Width) widthTemp++;
                    nbBytes = 8 + (iconSoundSize.Height * widthTemp);
                    buffer = new byte[nbBytes];
                    buffer[0] = 0x70; // Start code
                    buffer[1] = 0x82; // Icon code
                    buffer[2] = (byte)posx; // x (0 - 127)
                    buffer[3] = (byte)posy; // y (0 - 63)
                    buffer[4] = (byte)widthTemp; // Width
                    buffer[5] = (byte)iconSoundSize.Height; // Height
                    buffer[6] = 1; // vertical
                    buffer[7] = 16; // nbColors

                    for (j = 0; j < iconSoundSize.Height; j++)
                    {
                        for (i = 0; i < widthTemp; i++)
                        {
                            part1 = iconSound[(iconSoundSize.Height - 1) - j][i * 2 + 1];
                            if (i + 1 < iconSoundSize.Width)
                                part2 = iconSound[(iconSoundSize.Height - 1) - j][i * 2];
                            else
                                part2 = 0;
                            buffer[8 + i + j * widthTemp] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                        }
                    }
                    parent.serialPort1.Write(buffer, 0, nbBytes);
                    */
                }
                else if (icon == "Play")
                {
                    widthTemp = iconPlaySize.Width / 2;
                    if (widthTemp * 2 < iconPlaySize.Width) widthTemp++;
                    nbBytes = 8 + (iconPlaySize.Height * widthTemp);
                    buffer = new byte[nbBytes];
                    buffer[0] = 0x70; // Start code
                    buffer[1] = 0x82; // Icon code
                    buffer[2] = (byte)posx; // x (0 - 127)
                    buffer[3] = (byte)posy; // y (0 - 63)
                    buffer[4] = (byte)widthTemp; // Width
                    buffer[5] = (byte)iconPlaySize.Height; // Height
                    buffer[6] = 1; // vertical

                    for (j = 0; j < iconPlaySize.Height; j++)
                    {
                        for (i = 0; i < widthTemp; i++)
                        {
                            part1 = iconPlay[(iconPlaySize.Height - 1) - j][i * 2 + 1];
                            if (i + 1 < iconPlaySize.Width)
                                part2 = iconPlay[(iconPlaySize.Height - 1) - j][i * 2];
                            else
                                part2 = 0;
                            buffer[7 + i + j * widthTemp] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                        }
                    }
                    buffer[nbBytes - 1] = 0;

                    parent.serialPort1.Write(buffer, 0, nbBytes);
                    
                    /*
                    widthTemp = iconPlaySize.Width / 2;
                    if (widthTemp * 2 < iconPlaySize.Width) widthTemp++;
                    nbBytes = 8 + (iconPlaySize.Height * widthTemp);
                    buffer = new byte[nbBytes];
                    buffer[0] = 0x70; // Start code
                    buffer[1] = 0x82; // Icon code
                    buffer[2] = (byte)posx; // x (0 - 127)
                    buffer[3] = (byte)posy; // y (0 - 63)
                    buffer[4] = (byte)widthTemp; // Width
                    buffer[5] = (byte)iconPlaySize.Height; // Height
                    buffer[6] = 1; // vertical
                    buffer[7] = 16; // nbColors

                    for (j = 0; j < iconPlaySize.Height; j++)
                    {
                        for (i = 0; i < widthTemp; i++)
                        {
                            part1 = iconPlay[(iconPlaySize.Height - 1) - j][i * 2 + 1];
                            if (i + 1 < iconPlaySize.Width)
                                part2 = iconPlay[(iconPlaySize.Height - 1) - j][i * 2];
                            else
                                part2 = 0;
                            buffer[8 + i + j * widthTemp] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                        }
                    }
                    parent.serialPort1.Write(buffer, 0, nbBytes);
                    */
                }
                else if (icon == "Pause")
                {
                    widthTemp = iconPauseSize.Width / 2;
                    if (widthTemp * 2 < iconPauseSize.Width) widthTemp++;
                    nbBytes = 8 + (iconPauseSize.Height * widthTemp);
                    buffer = new byte[nbBytes];
                    buffer[0] = 0x70; // Start code
                    buffer[1] = 0x82; // Icon code
                    buffer[2] = (byte)posx; // x (0 - 127)
                    buffer[3] = (byte)posy; // y (0 - 63)
                    buffer[4] = (byte)widthTemp; // Width
                    buffer[5] = (byte)iconPauseSize.Height; // Height
                    buffer[6] = 1; // vertical

                    for (j = 0; j < iconPauseSize.Height; j++)
                    {
                        for (i = 0; i < widthTemp; i++)
                        {
                            part1 = iconPause[(iconPauseSize.Height - 1) - j][i * 2 + 1];
                            if (i + 1 < iconPauseSize.Width)
                                part2 = iconPause[(iconPauseSize.Height - 1) - j][i * 2];
                            else
                                part2 = 0;
                            buffer[7 + i + j * widthTemp] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                        }
                    }
                    buffer[nbBytes - 1] = 0;

                    parent.serialPort1.Write(buffer, 0, nbBytes);

                    /*
                    widthTemp = iconPauseSize.Width / 2;
                    if (widthTemp * 2 < iconPauseSize.Width) widthTemp++;
                    nbBytes = 8 + (iconPauseSize.Height * widthTemp);
                    buffer = new byte[nbBytes];
                    buffer[0] = 0x70; // Start code
                    buffer[1] = 0x82; // Icon code
                    buffer[2] = (byte)posx; // x (0 - 127)
                    buffer[3] = (byte)posy; // y (0 - 63)
                    buffer[4] = (byte)widthTemp; // Width
                    buffer[5] = (byte)iconPauseSize.Height; // Height
                    buffer[6] = 1; // vertical
                    buffer[7] = 16; // nbColors

                    for (j = 0; j < iconPauseSize.Height; j++)
                    {
                        for (i = 0; i < widthTemp; i++)
                        {
                            part1 = iconPause[(iconPauseSize.Height - 1) - j][i * 2 + 1];
                            if (i + 1 < iconPauseSize.Width)
                                part2 = iconPause[(iconPauseSize.Height - 1) - j][i * 2];
                            else
                                part2 = 0;
                            buffer[8 + i + j * widthTemp] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                        }
                    }
                    parent.serialPort1.Write(buffer, 0, nbBytes);
                    */
                }
                else if (icon == "Stop")
                {
                    widthTemp = iconStopSize.Width / 2;
                    if (widthTemp * 2 < iconStopSize.Width) widthTemp++;
                    nbBytes = 8 + (iconStopSize.Height * widthTemp);
                    buffer = new byte[nbBytes];
                    buffer[0] = 0x70; // Start code
                    buffer[1] = 0x82; // Icon code
                    buffer[2] = (byte)posx; // x (0 - 127)
                    buffer[3] = (byte)posy; // y (0 - 63)
                    buffer[4] = (byte)widthTemp; // Width
                    buffer[5] = (byte)iconStopSize.Height; // Height
                    buffer[6] = 1; // vertical

                    for (j = 0; j < iconStopSize.Height; j++)
                    {
                        for (i = 0; i < widthTemp; i++)
                        {
                            part1 = iconStop[(iconStopSize.Height - 1) - j][i * 2 + 1];
                            if (i + 1 < iconStopSize.Width)
                                part2 = iconStop[(iconStopSize.Height - 1) - j][i * 2];
                            else
                                part2 = 0;
                            buffer[7 + i + j * widthTemp] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                        }
                    }
                    buffer[nbBytes - 1] = 0;

                    parent.serialPort1.Write(buffer, 0, nbBytes);

                    /*
                    widthTemp = iconStopSize.Width / 2;
                    if (widthTemp * 2 < iconStopSize.Width) widthTemp++;
                    nbBytes = 8 + (iconStopSize.Height * widthTemp);
                    buffer = new byte[nbBytes];
                    buffer[0] = 0x70; // Start code
                    buffer[1] = 0x82; // Icon code
                    buffer[2] = (byte)posx; // x (0 - 127)
                    buffer[3] = (byte)posy; // y (0 - 63)
                    buffer[4] = (byte)widthTemp; // Width
                    buffer[5] = (byte)iconStopSize.Height; // Height
                    buffer[6] = 1; // vertical
                    buffer[7] = 16; // nbColors

                    for (j = 0; j < iconStopSize.Height; j++)
                    {
                        for (i = 0; i < widthTemp; i++)
                        {
                            part1 = iconStop[(iconStopSize.Height - 1) - j][i * 2 + 1];
                            if (i + 1 < iconStopSize.Width)
                                part2 = iconStop[(iconStopSize.Height - 1) - j][i * 2];
                            else
                                part2 = 0;
                            buffer[8 + i + j * widthTemp] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                        }
                    }
                    parent.serialPort1.Write(buffer, 0, nbBytes);
                    */
                }
                else if (icon == "Record")
                {
                    widthTemp = iconRecordSize.Width / 2;
                    if (widthTemp * 2 < iconRecordSize.Width) widthTemp++;
                    nbBytes = 8 + (iconRecordSize.Height * widthTemp);
                    buffer = new byte[nbBytes];
                    buffer[0] = 0x70; // Start code
                    buffer[1] = 0x82; // Icon code
                    buffer[2] = (byte)posx; // x (0 - 127)
                    buffer[3] = (byte)posy; // y (0 - 63)
                    buffer[4] = (byte)widthTemp; // Width
                    buffer[5] = (byte)iconRecordSize.Height; // Height
                    buffer[6] = 1; // vertical

                    for (j = 0; j < iconRecordSize.Height; j++)
                    {
                        for (i = 0; i < widthTemp; i++)
                        {
                            part1 = iconRecord[(iconRecordSize.Height - 1) - j][i * 2 + 1];
                            if (i + 1 < iconRecordSize.Width)
                                part2 = iconRecord[(iconRecordSize.Height - 1) - j][i * 2];
                            else
                                part2 = 0;
                            buffer[7 + i + j * widthTemp] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                        }
                    }
                    buffer[nbBytes - 1] = 0;

                    parent.serialPort1.Write(buffer, 0, nbBytes);


                    /*
                    widthTemp = iconRecordSize.Width / 2;
                    if (widthTemp * 2 < iconRecordSize.Width) widthTemp++;
                    nbBytes = 8 + (iconRecordSize.Height * widthTemp);
                    buffer = new byte[nbBytes];
                    buffer[0] = 0x70; // Start code
                    buffer[1] = 0x82; // Icon code
                    buffer[2] = (byte)posx; // x (0 - 127)
                    buffer[3] = (byte)posy; // y (0 - 63)
                    buffer[4] = (byte)widthTemp; // Width
                    buffer[5] = (byte)iconRecordSize.Height; // Height
                    buffer[6] = 1; // vertical
                    buffer[7] = 16; // nbColors

                    for (j = 0; j < iconRecordSize.Height; j++)
                    {
                        for (i = 0; i < widthTemp; i++)
                        {
                            part1 = iconRecord[(iconRecordSize.Height - 1) - j][i * 2 + 1];
                            if (i + 1 < iconRecordSize.Width)
                                part2 = iconRecord[(iconRecordSize.Height - 1) - j][i * 2];
                            else
                                part2 = 0;
                            buffer[8 + i + j * widthTemp] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                        }
                    }
                    parent.serialPort1.Write(buffer, 0, nbBytes);
                    */
                }
                else if (icon == "RW")
                {
                    widthTemp = iconRWSize.Width / 2;
                    if (widthTemp * 2 < iconRWSize.Width) widthTemp++;
                    nbBytes = 8 + (iconRWSize.Height * widthTemp);
                    buffer = new byte[nbBytes];
                    buffer[0] = 0x70; // Start code
                    buffer[1] = 0x82; // Icon code
                    buffer[2] = (byte)posx; // x (0 - 127)
                    buffer[3] = (byte)posy; // y (0 - 63)
                    buffer[4] = (byte)widthTemp; // Width
                    buffer[5] = (byte)iconRWSize.Height; // Height
                    buffer[6] = 1; // vertical

                    for (j = 0; j < iconRWSize.Height; j++)
                    {
                        for (i = 0; i < widthTemp; i++)
                        {
                            part1 = iconRW[(iconRWSize.Height - 1) - j][i * 2 + 1];
                            if (i + 1 < iconRWSize.Width)
                                part2 = iconRW[(iconRWSize.Height - 1) - j][i * 2];
                            else
                                part2 = 0;
                            buffer[7 + i + j * widthTemp] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                        }
                    }
                    buffer[nbBytes - 1] = 0;

                    parent.serialPort1.Write(buffer, 0, nbBytes);


                    /*
                    widthTemp = iconRWSize.Width / 2;
                    if (widthTemp * 2 < iconRWSize.Width) widthTemp++;
                    nbBytes = 8 + (iconRWSize.Height * widthTemp);
                    buffer = new byte[nbBytes];
                    buffer[0] = 0x70; // Start code
                    buffer[1] = 0x82; // Icon code
                    buffer[2] = (byte)posx; // x (0 - 127)
                    buffer[3] = (byte)posy; // y (0 - 63)
                    buffer[4] = (byte)widthTemp; // Width
                    buffer[5] = (byte)iconRWSize.Height; // Height
                    buffer[6] = 1; // vertical
                    buffer[7] = 16; // nbColors

                    for (j = 0; j < iconRWSize.Height; j++)
                    {
                        for (i = 0; i < widthTemp; i++)
                        {
                            part1 = iconRW[(iconRWSize.Height - 1) - j][i * 2 + 1];
                            if (i + 1 < iconRWSize.Width)
                                part2 = iconRW[(iconRWSize.Height - 1) - j][i * 2];
                            else
                                part2 = 0;
                            buffer[8 + i + j * widthTemp] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                        }
                    }
                    parent.serialPort1.Write(buffer, 0, nbBytes);
                    */
                }
                else if (icon == "FF")
                {
                    widthTemp = iconFFSize.Width / 2;
                    if (widthTemp * 2 < iconFFSize.Width) widthTemp++;
                    nbBytes = 8 + (iconFFSize.Height * widthTemp);
                    buffer = new byte[nbBytes];
                    buffer[0] = 0x70; // Start code
                    buffer[1] = 0x82; // Icon code
                    buffer[2] = (byte)posx; // x (0 - 127)
                    buffer[3] = (byte)posy; // y (0 - 63)
                    buffer[4] = (byte)widthTemp; // Width
                    buffer[5] = (byte)iconFFSize.Height; // Height
                    buffer[6] = 1; // vertical

                    for (j = 0; j < iconFFSize.Height; j++)
                    {
                        for (i = 0; i < widthTemp; i++)
                        {
                            part1 = iconFF[(iconFFSize.Height - 1) - j][i * 2 + 1];
                            if (i + 1 < iconFFSize.Width)
                                part2 = iconFF[(iconFFSize.Height - 1) - j][i * 2];
                            else
                                part2 = 0;
                            buffer[7 + i + j * widthTemp] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                        }
                    }
                    buffer[nbBytes - 1] = 0;

                    parent.serialPort1.Write(buffer, 0, nbBytes);

                    /*
                    widthTemp = iconFFSize.Width / 2;
                    if (widthTemp * 2 < iconFFSize.Width) widthTemp++;
                    nbBytes = 8 + (iconFFSize.Height * widthTemp);
                    buffer = new byte[nbBytes];
                    buffer[0] = 0x70; // Start code
                    buffer[1] = 0x82; // Icon code
                    buffer[2] = (byte)posx; // x (0 - 127)
                    buffer[3] = (byte)posy; // y (0 - 63)
                    buffer[4] = (byte)widthTemp; // Width
                    buffer[5] = (byte)iconFFSize.Height; // Height
                    buffer[6] = 1; // vertical
                    buffer[7] = 16; // nbColors

                    for (j = 0; j < iconFFSize.Height; j++)
                    {
                        for (i = 0; i < widthTemp; i++)
                        {
                            part1 = iconFF[(iconFFSize.Height - 1) - j][i * 2 + 1];
                            if (i + 1 < iconFFSize.Width)
                                part2 = iconFF[(iconFFSize.Height - 1) - j][i * 2];
                            else
                                part2 = 0;
                            buffer[8 + i + j * widthTemp] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                        }
                    }
                    parent.serialPort1.Write(buffer, 0, nbBytes);
                    */ 
                }
            }
        }
        public void sendProgressBar(int position, int posx, int posy)
        {
            if (parent.serialPort1.IsOpen)// && !parent.timerVolumeDisplay.Enabled )
            {
                int i, j;
                byte part1, part2;
                Pen myPen;
                Graphics g;
                Bitmap bitmap = new Bitmap(240, 5);
                g = Graphics.FromImage(bitmap);
                myPen = new Pen(Color.Gray, 1);
                g.DrawLine(myPen, 0, 0, position, 0);
                myPen = new Pen(Color.White, 1);
                g.DrawLine(myPen, 0, 1, position, 1);
                g.DrawLine(myPen, 0, 2, position, 2);
                g.DrawLine(myPen, 0, 3, position, 3);
                myPen = new Pen(Color.Gray, 1);
                g.DrawLine(myPen, 0, 4, position, 4);

                //-- add marquer at 0%,25%,50%,75%,100%
                if (parent.RCF.ProgressBarWithMarquer == 1)
                {
                    myPen = new Pen(Color.White, 1);
                    g.DrawLine(myPen, 60, 0, 60, 4);
                    g.DrawLine(myPen, 120, 0, 120, 4);
                    g.DrawLine(myPen, 180, 0, 180, 4);
                    g.DrawLine(myPen, 239, 0, 239, 4); //old: 237,0 237,4
                }

                //-- add border
                if (parent.RCF.ProgressBarWithBorder == 1)
                {
                    myPen = new Pen(Color.DarkGray, 1);
                    g.DrawLine(myPen, position, 0, 239, 0); //old: position , 0, 237, 0
                    g.DrawLine(myPen, position, 4, 239, 4); //old: position , 4, 237, 0
                    g.DrawLine(myPen, 239, 0, 239, 4); //old: 237 ,0 237, 0
                    g.DrawLine(myPen, 0, 0, 0, 4);
                }

                byte[] buffer = new byte[608];
                buffer[0] = 0x70; // Start code
                buffer[1] = 0x82; // Icon code
                buffer[2] = (byte)posx; // x (0 - 127)
                buffer[3] = (byte)posy; // y (0 - 63)
                buffer[4] = (byte)120; // Width
                buffer[5] = (byte)5; // Height
                buffer[6] = 1; // vertical

                for (j = 0; j < 5; j++)
                {
                    for (i = 0; i < 120; i++)
                    {
                        part1 = convertColor(System.Drawing.ColorTranslator.ToWin32(bitmap.GetPixel(i * 2 + 1, (4 - j))));
                        part2 = convertColor(System.Drawing.ColorTranslator.ToWin32(bitmap.GetPixel(i * 2, (4 - j))));
                        buffer[7 + i + j * 120] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                    }
                }
                buffer[607] = 0;

                parent.serialPort1.Write(buffer, 0, 608);

                /*
                byte[] buffer = new byte[608];
                buffer[0] = 0x70; // Start code
                buffer[1] = 0x82; // Icon code
                buffer[2] = (byte)posx; // x (0 - 127)
                buffer[3] = (byte)posy; // y (0 - 63)
                buffer[4] = (byte)120; // Width
                buffer[5] = (byte)5; // Height
                buffer[6] = 1; // vertical
                buffer[7] = 16; // nbColors

                for (j = 0; j < 5; j++)
                {
                    for (i = 0; i < 120; i++)
                    {
                        part1 = convertColor(System.Drawing.ColorTranslator.ToWin32(bitmap.GetPixel(i * 2 + 1, (4 - j))));
                        part2 = convertColor(System.Drawing.ColorTranslator.ToWin32(bitmap.GetPixel(i * 2, (4 - j))));
                        buffer[8 + i + j * 120] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                    }
                }
                parent.serialPort1.Write(buffer, 0, 608);
                */
            }
        }
        public void sendProgressBarSound(int position, int posx, int posy)
        {
            if (parent.serialPort1.IsOpen)// && !parent.timerVolumeDisplay.Enabled )
            {
                int i, j;
                byte part1, part2;
                Pen myPen;
                Graphics g;
                Bitmap bitmap = new Bitmap(120, 5);
                g = Graphics.FromImage(bitmap);
                myPen = new Pen(Color.Gray, 1);
                g.DrawLine(myPen, 0, 0, position, 0);
                myPen = new Pen(Color.White, 1);
                g.DrawLine(myPen, 0, 1, position, 1);
                g.DrawLine(myPen, 0, 2, position, 2);
                g.DrawLine(myPen, 0, 3, position, 3);
                myPen = new Pen(Color.Gray, 1);
                g.DrawLine(myPen, 0, 4, position, 4);

                myPen = new Pen(Color.DarkGray, 1);
                g.DrawLine(myPen, position, 0, 119, 0); //old: position, 0, 117, 0
                g.DrawLine(myPen, position, 4, 119, 4); //old: position, 0, 117, 0
                g.DrawLine(myPen, 119, 0, 119, 4); //old: position, 0, 117, 0
                g.DrawLine(myPen, 0, 0, 0, 4);

                byte[] buffer = new byte[308];
                buffer[0] = 0x70; // Start code
                buffer[1] = 0x82; // Icon code
                buffer[2] = (byte)posx; // x (0 - 127)
                buffer[3] = (byte)posy; // y (0 - 63)
                buffer[4] = (byte)60; // Width
                buffer[5] = (byte)5; // Height
                buffer[6] = 1; // vertical

                for (j = 0; j < 5; j++)
                {
                    for (i = 0; i < 60; i++)
                    {
                        part1 = convertColor(System.Drawing.ColorTranslator.ToWin32(bitmap.GetPixel(i * 2 + 1, (4 - j))));
                        part2 = convertColor(System.Drawing.ColorTranslator.ToWin32(bitmap.GetPixel(i * 2, (4 - j))));
                        buffer[7 + i + j * 60] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                    }
                }
                buffer[307] = 0;

                parent.serialPort1.Write(buffer, 0, 308);

                /*
                byte[] buffer = new byte[608];
                buffer[0] = 0x70; // Start code
                buffer[1] = 0x82; // Icon code
                buffer[2] = (byte)posx; // x (0 - 127)
                buffer[3] = (byte)posy; // y (0 - 63)
                buffer[4] = (byte)60; // Width
                buffer[5] = (byte)5; // Height
                buffer[6] = 1; // vertical
                buffer[7] = 16; // nbColors

                for (j = 0; j < 5; j++)
                {
                    for (i = 0; i < 60; i++)
                    {
                        part1 = convertColor(System.Drawing.ColorTranslator.ToWin32(bitmap.GetPixel(i * 2 + 1, (4 - j))));
                        part2 = convertColor(System.Drawing.ColorTranslator.ToWin32(bitmap.GetPixel(i * 2, (4 - j))));
                        buffer[8 + i + j * 60] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                    }
                }
                parent.serialPort1.Write(buffer, 0, 308);
                */
            }
        }
        
        //--
        public void sendBitmapText(string strToSend, int posx, int posy, int font, bool erase, bool canScroll)
        {
            byte part1, part2;
            Graphics g;
            Size textSize = System.Windows.Forms.TextRenderer.MeasureText(strToSend, parent.getTextFont(font));
            if ((textSize.Width < 256 && textSize.Height < 64) &&
                (textSize.Width != 0 && textSize.Height != 0))
            {
                Bitmap bitmap = new Bitmap(256, 64);
                g = Graphics.FromImage(bitmap);
                System.Windows.Forms.TextRenderer.DrawText(g, strToSend, parent.getTextFont(font), Point.Empty, Color.White, Color.Black);
                // Send the data contained in the bitmap to the serial port thanks to the getPixel method
                if (parent.serialPort1.IsOpen)
                {
                    int i, j;
                    int widthTemp;
                    if (erase)
                        widthTemp = 128;
                    else
                    {
                        if (textSize.Width >= 256)
                            widthTemp = 128;
                        else
                        {
                            widthTemp = textSize.Width / 2;
                            if (widthTemp * 2 < widthTemp) widthTemp++;
                        }
                    }

                    int nbBytes = 8 + (textSize.Height * widthTemp);
                    byte[] buffer = new byte[nbBytes];
                    buffer[0] = 0x70; // Start code
                    buffer[1] = 0x82; // Icon code
                    buffer[2] = (byte)posx; // x (0 - 127)
                    buffer[3] = (byte)posy; // y (0 - 63)
                    buffer[4] = (byte)widthTemp; // Width
                    buffer[5] = (byte)textSize.Height; // Height
                    buffer[6] = 1; // vertical

                    for (j = 0; j < textSize.Height; j++)
                    {
                        for (i = 0; i < widthTemp; i++)
                        {
                            part1 = convertColor(System.Drawing.ColorTranslator.ToWin32(bitmap.GetPixel(i * 2 + 1, (textSize.Height - j))));
                            if (i + 1 < textSize.Width)
                                part2 = convertColor(System.Drawing.ColorTranslator.ToWin32(bitmap.GetPixel(i * 2, (textSize.Height - j))));
                            else
                                part2 = 0;
                            buffer[7 + i + j * widthTemp] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                        }
                    }
                    buffer[nbBytes - 1] = 0;

                    parent.serialPort1.Write(buffer, 0, nbBytes);

                    /*
                    byte[] buffer = new byte[nbBytes];
                    buffer[0] = 0x70; // Start code
                    buffer[1] = 0x82; // Icon code
                    buffer[2] = (byte)posx; // x (0 - 127)
                    buffer[3] = (byte)posy; // y (0 - 63)
                    buffer[4] = (byte)widthTemp; // Width
                    buffer[5] = (byte)textSize.Height; // Height
                    buffer[6] = 1; // vertical
                    buffer[7] = 16; // nbColors

                    for (j = 0; j < textSize.Height; j++)
                    {
                        for (i = 0; i < widthTemp; i++)
                        {
                            part1 = convertColor(System.Drawing.ColorTranslator.ToWin32(bitmap.GetPixel(i * 2 + 1, (textSize.Height - j))));
                            if (i + 1 < textSize.Width)
                                part2 = convertColor(System.Drawing.ColorTranslator.ToWin32(bitmap.GetPixel(i * 2, (textSize.Height - j))));
                            else
                                part2 = 0;
                            buffer[8 + i + j * widthTemp] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                        }
                    }
                    parent.serialPort1.Write(buffer, 0, nbBytes);
                    */
                }
            }
            else if ((textSize.Width < 2048 && textSize.Height < 64) &&
                (textSize.Width != 0 && textSize.Height != 0) && canScroll)
            {
                scrollingText = new Bitmap(2048, 64);
                g = Graphics.FromImage(scrollingText);
                System.Windows.Forms.TextRenderer.DrawText(g, strToSend, parent.getTextFont(font), Point.Empty, Color.White, Color.Black);
                int widthTemp = 0;
                // Send the data contained in the bitmap to the serial port thanks to the getPixel method
                if (parent.serialPort1.IsOpen)
                {
                    int i, j;
                    widthTemp = textSize.Width / 2;
                    if (widthTemp * 2 < widthTemp)
                        widthTemp++;
                    int nbBytes = 8 + (textSize.Height * widthTemp);

                    byte[] buffer = new byte[nbBytes];
                    buffer[0] = 0x70; // Start code
                    buffer[1] = 0x82; // Icon code
                    buffer[2] = (byte)posx; // x (0 - 127)
                    buffer[3] = (byte)posy; // y (0 - 63)
                    buffer[4] = (byte)widthTemp; // Width
                    buffer[5] = (byte)textSize.Height; // Height
                    buffer[6] = 1; // vertical
                    buffer[7] = 16; // nbColors

                    for (j = 0; j < textSize.Height; j++)
                    {
                        for (i = 0; i < 128; i++)
                        {
                            part1 = convertColor(System.Drawing.ColorTranslator.ToWin32(scrollingText.GetPixel(i * 2 + 1, (textSize.Height - j))));
                            if (i + 1 < textSize.Width)
                                part2 = convertColor(System.Drawing.ColorTranslator.ToWin32(scrollingText.GetPixel(i * 2, (textSize.Height - j))));
                            else
                                part2 = 0;
                            buffer[7 + i + j * widthTemp] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                        }
                    }
                    buffer[nbBytes - 1] = 0;

                    parent.serialPort1.Write(buffer, 0, nbBytes);

                    /*
                    byte[] buffer = new byte[nbBytes];
                    buffer[0] = 0x70; // Start code
                    buffer[1] = 0x82; // Icon code
                    buffer[2] = (byte)posx; // x (0 - 127)
                    buffer[3] = (byte)posy; // y (0 - 63)
                    buffer[4] = (byte)widthTemp; // Width
                    buffer[5] = (byte)textSize.Height; // Height
                    buffer[6] = 1; // vertical
                    buffer[7] = 16; // nbColors

                    for (j = 0; j < textSize.Height; j++)
                    {
                        for (i = 0; i < 128; i++)
                        {
                            part1 = convertColor(System.Drawing.ColorTranslator.ToWin32(scrollingText.GetPixel(i * 2 + 1, (textSize.Height - j))));
                            if (i + 1 < textSize.Width)
                                part2 = convertColor(System.Drawing.ColorTranslator.ToWin32(scrollingText.GetPixel(i * 2, (textSize.Height - j))));
                            else
                                part2 = 0;
                            buffer[8 + i + j * widthTemp] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                        }
                    }
                    parent.serialPort1.Write(buffer, 0, nbBytes);
                    */
                }
                scrollingTextPosition = 0;
                scrollingRightToLeft = true;
                scrollingPosX = posx;
                scrollingPosY = posy;
                scrollingTextHeight = textSize.Height;
                scrollingTextWidth = widthTemp;
                if (!parent.timerScrollingText.IsEnabled)
                    parent.timerScrollingText.IsEnabled = true;
            }
            else
            {
                //Log.Write("Texte trop grand");
            }
        }
        public void updateScrollingText()
        {
            if (parent.serialPort1.IsOpen && !parent.timerVolumeDisplay.IsEnabled)// && (parent.mediaState.MediaCenter.Navigation != MediaStatusPropertyTag.FS_Home))
            {
                #region "Scrolling Left and Right"
                //--
                if (parent.RCF.TextDisplayRightAndLeft == 1)//(scrollingLeftAndRight)
                {
                    if (scrollingRightToLeft)
                    {
                        scrollingTextPosition += 2;
                        if (scrollingTextPosition > 2 * (scrollingTextWidth - 128))
                        {
                            scrollingRightToLeft = false;
                            scrollingTextPosition -= 2;
                        }
                    }
                    else if (!scrollingRightToLeft)
                    {
                        scrollingTextPosition -= 2;
                        if (scrollingTextPosition <= 0)
                        {
                            scrollingRightToLeft = true;
                            scrollingTextPosition = 0;
                        }
                    }

                    int i, j;
                    byte part1, part2;
                    int nbBytes = 8 + (scrollingTextHeight * 128);

                    byte[] buffer = new byte[nbBytes];
                    buffer[0] = 0x70; // Start code
                    buffer[1] = 0x82; // Icon code
                    buffer[2] = (byte)scrollingPosX; // x (0 - 127)
                    buffer[3] = (byte)scrollingPosY; // y (0 - 63)
                    buffer[4] = (byte)128; // Width
                    buffer[5] = (byte)scrollingTextHeight; // Height
                    buffer[6] = 1; // vertical

                    for (j = 0; j < scrollingTextHeight; j++)
                    {
                        for (i = 0; i < 128; i++)
                        {
                            part1 = convertColor(System.Drawing.ColorTranslator.ToWin32(scrollingText.GetPixel(i * 2 + 1 + scrollingTextPosition, (scrollingTextHeight - j))));
                            if (i + 1 < 128)
                                part2 = convertColor(System.Drawing.ColorTranslator.ToWin32(scrollingText.GetPixel(i * 2 + scrollingTextPosition, (scrollingTextHeight - j))));
                            else
                                part2 = 0;
                            buffer[7 + i + j * 128] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                        }
                    }
                    buffer[nbBytes - 1] = 0;

                    parent.serialPort1.Write(buffer, 0, nbBytes);

                    /*
                    byte[] buffer = new byte[nbBytes];
                    buffer[0] = 0x70; // Start code
                    buffer[1] = 0x82; // Icon code
                    buffer[2] = (byte)scrollingPosX; // x (0 - 127)
                    buffer[3] = (byte)scrollingPosY; // y (0 - 63)
                    buffer[4] = (byte)128; // Width
                    buffer[5] = (byte)scrollingTextHeight; // Height
                    buffer[6] = 1; // vertical
                    buffer[7] = 16; // nbColors

                    for (j = 0; j < scrollingTextHeight; j++)
                    {
                        for (i = 0; i < 128; i++)
                        {
                            part1 = convertColor(System.Drawing.ColorTranslator.ToWin32(scrollingText.GetPixel(i * 2 + 1 + scrollingTextPosition, (scrollingTextHeight - j))));
                            if (i + 1 < 128)
                                part2 = convertColor(System.Drawing.ColorTranslator.ToWin32(scrollingText.GetPixel(i * 2 + scrollingTextPosition, (scrollingTextHeight - j))));
                            else
                                part2 = 0;
                            buffer[8 + i + j * 128] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                        }
                    }
                    parent.serialPort1.Write(buffer, 0, nbBytes);
                    */
                }
                //--
                #endregion "Scrolling Left and Right" 

                #region "Scrolling cycle"
                //--
                else
                {
                    if (scrollingRightToLeft)
                    {
                        scrollingTextPosition += 2;
                        if (scrollingTextPosition > 2 * (scrollingTextWidth))//(scrollingTextPosition > 2 * (scrollingTextWidth - 128))
                        {
                            scrollingRightToLeft = false;
                            scrollingTextPosition = -256;//-= 2;
                        }

                        int i, j;
                        byte part1, part2;
                        int nbBytes = 8 + (scrollingTextHeight * 128);

                        byte[] buffer = new byte[nbBytes];
                        buffer[0] = 0x70; // Start code
                        buffer[1] = 0x82; // Icon code
                        buffer[2] = (byte)scrollingPosX; // x (0 - 127)
                        buffer[3] = (byte)scrollingPosY; // y (0 - 63)
                        buffer[4] = (byte)128; // Width
                        buffer[5] = (byte)scrollingTextHeight; // Height
                        buffer[6] = 1; // vertical

                        for (j = 0; j < scrollingTextHeight; j++)
                        {
                            for (i = 0; i < 128; i++)
                            {
                                if (i + scrollingTextPosition < 0)
                                {
                                    part1 = convertColor(System.Drawing.ColorTranslator.ToWin32(System.Drawing.Color.Black));
                                    part2 = convertColor(System.Drawing.ColorTranslator.ToWin32(System.Drawing.Color.Black));
                                }
                                else
                                {
                                    part1 = convertColor(System.Drawing.ColorTranslator.ToWin32(scrollingText.GetPixel(i * 2 + 1 + scrollingTextPosition, (scrollingTextHeight - j))));
                                    if (i + 1 < 128)
                                        part2 = convertColor(System.Drawing.ColorTranslator.ToWin32(scrollingText.GetPixel(i * 2 + scrollingTextPosition, (scrollingTextHeight - j))));
                                    else
                                        part2 = 0;
                                }
                                buffer[7 + i + j * 128] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                            }
                        }
                        buffer[nbBytes - 1] = 0;

                        parent.serialPort1.Write(buffer, 0, nbBytes);

                        /*
                        byte[] buffer = new byte[nbBytes];
                        buffer[0] = 0x70; // Start code
                        buffer[1] = 0x82; // Icon code
                        buffer[2] = (byte)scrollingPosX; // x (0 - 127)
                        buffer[3] = (byte)scrollingPosY; // y (0 - 63)
                        buffer[4] = (byte)128; // Width
                        buffer[5] = (byte)scrollingTextHeight; // Height
                        buffer[6] = 1; // vertical
                        buffer[7] = 16; // nbColors

                        for (j = 0; j < scrollingTextHeight; j++)
                        {
                            for (i = 0; i < 128; i++)
                            {
                                if (i + scrollingTextPosition < 0)
                                {
                                    part1 = convertColor(System.Drawing.ColorTranslator.ToWin32(System.Drawing.Color.Black));
                                    part2 = convertColor(System.Drawing.ColorTranslator.ToWin32(System.Drawing.Color.Black));
                                }
                                else
                                {
                                    part1 = convertColor(System.Drawing.ColorTranslator.ToWin32(scrollingText.GetPixel(i * 2 + 1 + scrollingTextPosition, (scrollingTextHeight - j))));
                                    if (i + 1 < 128)
                                        part2 = convertColor(System.Drawing.ColorTranslator.ToWin32(scrollingText.GetPixel(i * 2 + scrollingTextPosition, (scrollingTextHeight - j))));
                                    else
                                        part2 = 0;
                                }
                                buffer[8 + i + j * 128] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                            }
                        }
                        parent.serialPort1.Write(buffer, 0, nbBytes);
                        */
                    }
                    else if (!scrollingRightToLeft)
                    {
                        scrollingTextPosition += 2;
                        if (scrollingTextPosition >= 0)
                        {
                            scrollingRightToLeft = true;
                            scrollingTextPosition = 0;
                        }

                        int i, j;
                        byte part1, part2;
                        int nbBytes = 8 + (scrollingTextHeight * 128);

                        byte[] buffer = new byte[nbBytes];
                        buffer[0] = 0x70; // Start code
                        buffer[1] = 0x82; // Icon code
                        buffer[2] = (byte)scrollingPosX; // x (0 - 127)
                        buffer[3] = (byte)scrollingPosY; // y (0 - 63)
                        buffer[4] = (byte)128; // Width
                        buffer[5] = (byte)scrollingTextHeight; // Height
                        buffer[6] = 1; // vertical

                        for (j = 0; j < scrollingTextHeight; j++)
                        {
                            for (i = 0; i < 128; i++)
                            {
                                if (i * 2 + 1 + scrollingTextPosition < 0)//(i + scrollingTextPosition < 0)
                                {
                                    part1 = convertColor(System.Drawing.ColorTranslator.ToWin32(System.Drawing.Color.Black));
                                    part2 = convertColor(System.Drawing.ColorTranslator.ToWin32(System.Drawing.Color.Black));
                                }
                                else
                                {
                                    part1 = convertColor(System.Drawing.ColorTranslator.ToWin32(scrollingText.GetPixel(i * 2 + 1 + scrollingTextPosition, (scrollingTextHeight - j))));
                                    if (i + 1 < 128)
                                        part2 = convertColor(System.Drawing.ColorTranslator.ToWin32(scrollingText.GetPixel(i * 2 + scrollingTextPosition, (scrollingTextHeight - j))));
                                    else
                                        part2 = 0;
                                }
                                buffer[7 + i + j * 128] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                            }
                        }
                        buffer[nbBytes - 1] = 0;

                        parent.serialPort1.Write(buffer, 0, nbBytes);

                        /*
                        byte[] buffer = new byte[nbBytes];
                        buffer[0] = 0x70; // Start code
                        buffer[1] = 0x82; // Icon code
                        buffer[2] = (byte)scrollingPosX; // x (0 - 127)
                        buffer[3] = (byte)scrollingPosY; // y (0 - 63)
                        buffer[4] = (byte)128; // Width
                        buffer[5] = (byte)scrollingTextHeight; // Height
                        buffer[6] = 1; // vertical
                        buffer[7] = 16; // nbColors

                        for (j = 0; j < scrollingTextHeight; j++)
                        {
                            for (i = 0; i < 128; i++)
                            {
                                if (i * 2 + 1 + scrollingTextPosition < 0)//(i + scrollingTextPosition < 0)
                                {
                                    part1 = convertColor(System.Drawing.ColorTranslator.ToWin32(System.Drawing.Color.Black));
                                    part2 = convertColor(System.Drawing.ColorTranslator.ToWin32(System.Drawing.Color.Black));
                                }
                                else
                                {
                                    part1 = convertColor(System.Drawing.ColorTranslator.ToWin32(scrollingText.GetPixel(i * 2 + 1 + scrollingTextPosition, (scrollingTextHeight - j))));
                                    if (i + 1 < 128)
                                        part2 = convertColor(System.Drawing.ColorTranslator.ToWin32(scrollingText.GetPixel(i * 2 + scrollingTextPosition, (scrollingTextHeight - j))));
                                    else
                                        part2 = 0;
                                }
                                buffer[8 + i + j * 128] = (byte)((byte)(part1 & 0xF0) + (byte)(part2 >> 4));
                            }
                        }
                        parent.serialPort1.Write(buffer, 0, nbBytes);
                        */
                    }

                }
                //--
                #endregion "Scrolling cycle"
            }
        }
        private byte convertColor(int colorWin32)
        {
            int red = colorWin32 & 255;
            int green = (colorWin32 & 65280) >> 8;
            int blue = (colorWin32 & 16711680) >> 16;
            int mean = (red + green + blue) / 3;
            return (byte)mean;//(mean / 3 + 170);
        }
        public void loadIconsFolder()
        {
            int i, x, y;
            System.Drawing.Bitmap myImage;
            iconsAreLoaded = false;


            myImage = new System.Drawing.Bitmap(parent.getIconsFolder() + "D-Image.bmp");
            iconBkgnd_Death = new byte[64][];
            for (i = 0; i < 64; i++)
                iconBkgnd_Death[i] = new byte[256];
            for (y = 0; y < 64; y++)
            {
                for (x = 0; x < 256; x++)
                    iconBkgnd_Death[y][x] = (byte)convertColor(System.Drawing.ColorTranslator.ToWin32(myImage.GetPixel(x, y)));
            }

            myImage = new System.Drawing.Bitmap(parent.getIconsFolder() + "Bkgnd_DVD.bmp");
            iconBkgnd_DVD = new byte[64][];
            for (i = 0; i < 64; i++)
                iconBkgnd_DVD[i] = new byte[256];
            for (y = 0; y < 64; y++)
            {
                for (x = 0; x < 256; x++)
                    iconBkgnd_DVD[y][x] = (byte)convertColor(System.Drawing.ColorTranslator.ToWin32(myImage.GetPixel(x, y)));
            }

            myImage = new System.Drawing.Bitmap(parent.getIconsFolder() + "Bkgnd_CD.bmp");
            iconBkgnd_CD = new byte[64][];
            for (i = 0; i < 64; i++)
                iconBkgnd_CD[i] = new byte[256];
            for (y = 0; y < 64; y++)
            {
                for (x = 0; x < 256; x++)
                    iconBkgnd_CD[y][x] = (byte)convertColor(System.Drawing.ColorTranslator.ToWin32(myImage.GetPixel(x, y)));
            }

            myImage = new System.Drawing.Bitmap(parent.getIconsFolder() + "Bkgnd_PVR.bmp");
            iconBkgnd_PVR = new byte[64][];
            for (i = 0; i < 64; i++)
                iconBkgnd_PVR[i] = new byte[256];
            for (y = 0; y < 64; y++)
            {
                for (x = 0; x < 256; x++)
                    iconBkgnd_PVR[y][x] = (byte)convertColor(System.Drawing.ColorTranslator.ToWin32(myImage.GetPixel(x, y)));
            }

            myImage = new System.Drawing.Bitmap(parent.getIconsFolder() + "Bkgnd_Audio.bmp");
            iconBkgnd_Audio = new byte[64][];
            for (i = 0; i < 64; i++)
                iconBkgnd_Audio[i] = new byte[256];
            for (y = 0; y < 64; y++)
            {
                for (x = 0; x < 256; x++)
                    iconBkgnd_Audio[y][x] = (byte)convertColor(System.Drawing.ColorTranslator.ToWin32(myImage.GetPixel(x, y)));
            }

            myImage = new System.Drawing.Bitmap(parent.getIconsFolder() + "Bkgnd_Video.bmp");
            iconBkgnd_Video = new byte[64][];
            for (i = 0; i < 64; i++)
                iconBkgnd_Video[i] = new byte[256];
            for (y = 0; y < 64; y++)
            {
                for (x = 0; x < 256; x++)
                    iconBkgnd_Video[y][x] = (byte)convertColor(System.Drawing.ColorTranslator.ToWin32(myImage.GetPixel(x, y)));
            }

            myImage = new System.Drawing.Bitmap(parent.getIconsFolder() + "Bkgnd_TVTuner.bmp");
            iconBkgnd_TVTuner = new byte[64][];
            for (i = 0; i < 64; i++)
                iconBkgnd_TVTuner[i] = new byte[256];
            for (y = 0; y < 64; y++)
            {
                for (x = 0; x < 256; x++)
                    iconBkgnd_TVTuner[y][x] = (byte)convertColor(System.Drawing.ColorTranslator.ToWin32(myImage.GetPixel(x, y)));
            }

            myImage = new System.Drawing.Bitmap(parent.getIconsFolder() + "Bkgnd_Home.bmp");
            iconBkgnd_Home = new byte[64][];
            for (i = 0; i < 64; i++)
                iconBkgnd_Home[i] = new byte[256];
            for (y = 0; y < 64; y++)
            {
                for (x = 0; x < 256; x++)
                    iconBkgnd_Home[y][x] = (byte)convertColor(System.Drawing.ColorTranslator.ToWin32(myImage.GetPixel(x, y)));
            }

            myImage = new System.Drawing.Bitmap(parent.getIconsFolder() + "Bkgnd_Home2.bmp");
            iconBkgnd_Home2 = new byte[64][];
            for (i = 0; i < 64; i++)
                iconBkgnd_Home2[i] = new byte[256];
            for (y = 0; y < 64; y++)
            {
                for (x = 0; x < 256; x++)
                    iconBkgnd_Home2[y][x] = (byte)convertColor(System.Drawing.ColorTranslator.ToWin32(myImage.GetPixel(x, y)));
            }
            
            myImage = new System.Drawing.Bitmap(parent.getIconsFolder() + "Bkgnd_Photos.bmp");
            iconBkgnd_Photos = new byte[64][];
            for (i = 0; i < 64; i++)
                iconBkgnd_Photos[i] = new byte[256];
            for (y = 0; y < 64; y++)
            {
                for (x = 0; x < 256; x++)
                    iconBkgnd_Photos[y][x] = (byte)convertColor(System.Drawing.ColorTranslator.ToWin32(myImage.GetPixel(x, y)));
            }

            myImage = new System.Drawing.Bitmap(parent.getIconsFolder() + "Bkgnd_Radio.bmp");
            iconBkgnd_Radio = new byte[64][];
            for (i = 0; i < 64; i++)
                iconBkgnd_Radio[i] = new byte[256];
            for (y = 0; y < 64; y++)
            {
                for (x = 0; x < 256; x++)
                    iconBkgnd_Radio[y][x] = (byte)convertColor(System.Drawing.ColorTranslator.ToWin32(myImage.GetPixel(x, y)));
            }

            myImage = new System.Drawing.Bitmap(parent.getIconsFolder() + "Sound.bmp");
            iconSoundSize = new Size(myImage.Width, myImage.Height);
            iconSound = new byte[myImage.Height][];
            for (i = 0; i < myImage.Height; i++)
                iconSound[i] = new byte[myImage.Width];
            for (y = 0; y < myImage.Height; y++)
            {
                for (x = 0; x < myImage.Width; x++)
                    iconSound[y][x] = (byte)convertColor(System.Drawing.ColorTranslator.ToWin32(myImage.GetPixel(x, y)));
            }

            myImage = new System.Drawing.Bitmap(parent.getIconsFolder() + "Play.bmp");
            iconPlaySize = new Size(myImage.Width, myImage.Height);
            iconPlay = new byte[myImage.Height][];
            for (i = 0; i < myImage.Height; i++)
                iconPlay[i] = new byte[myImage.Width];
            for (y = 0; y < myImage.Height; y++)
            {
                for (x = 0; x < myImage.Width; x++)
                    iconPlay[y][x] = (byte)convertColor(System.Drawing.ColorTranslator.ToWin32(myImage.GetPixel(x, y)));
            }

            myImage = new System.Drawing.Bitmap(parent.getIconsFolder() + "Pause.bmp");
            iconPauseSize = new Size(myImage.Width, myImage.Height);
            iconPause = new byte[myImage.Height][];
            for (i = 0; i < myImage.Height; i++)
                iconPause[i] = new byte[myImage.Width];
            for (y = 0; y < myImage.Height; y++)
            {
                for (x = 0; x < myImage.Width; x++)
                    iconPause[y][x] = (byte)convertColor(System.Drawing.ColorTranslator.ToWin32(myImage.GetPixel(x, y)));
            }

            myImage = new System.Drawing.Bitmap(parent.getIconsFolder() + "Stop.bmp");
            iconStopSize = new Size(myImage.Width, myImage.Height);
            iconStop = new byte[myImage.Height][];
            for (i = 0; i < myImage.Height; i++)
                iconStop[i] = new byte[myImage.Width];
            for (y = 0; y < myImage.Height; y++)
            {
                for (x = 0; x < myImage.Width; x++)
                    iconStop[y][x] = (byte)convertColor(System.Drawing.ColorTranslator.ToWin32(myImage.GetPixel(x, y)));
            }

            myImage = new System.Drawing.Bitmap(parent.getIconsFolder() + "Record.bmp");
            iconRecordSize = new Size(myImage.Width, myImage.Height);
            iconRecord = new byte[myImage.Height][];
            for (i = 0; i < myImage.Height; i++)
                iconRecord[i] = new byte[myImage.Width];
            for (y = 0; y < myImage.Height; y++)
            {
                for (x = 0; x < myImage.Width; x++)
                    iconRecord[y][x] = (byte)convertColor(System.Drawing.ColorTranslator.ToWin32(myImage.GetPixel(x, y)));
            }

            myImage = new System.Drawing.Bitmap(parent.getIconsFolder() + "RW.bmp");
            iconRWSize = new Size(myImage.Width, myImage.Height);
            iconRW = new byte[myImage.Height][];
            for (i = 0; i < myImage.Height; i++)
                iconRW[i] = new byte[myImage.Width];
            for (y = 0; y < myImage.Height; y++)
            {
                for (x = 0; x < myImage.Width; x++)
                    iconRW[y][x] = (byte)convertColor(System.Drawing.ColorTranslator.ToWin32(myImage.GetPixel(x, y)));
            }

            myImage = new System.Drawing.Bitmap(parent.getIconsFolder() + "FF.bmp");
            iconFFSize = new Size(myImage.Width, myImage.Height);
            iconFF = new byte[myImage.Height][];
            for (i = 0; i < myImage.Height; i++)
                iconFF[i] = new byte[myImage.Width];
            for (y = 0; y < myImage.Height; y++)
            {
                for (x = 0; x < myImage.Width; x++)
                    iconFF[y][x] = (byte)convertColor(System.Drawing.ColorTranslator.ToWin32(myImage.GetPixel(x, y)));
            }

            iconsAreLoaded = true;
        }
        //--
        #endregion "Methods"
    }
}
